package com.epam.newsmanagement.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import com.epam.newsmanagement.exception.NewsManagementException;


public final class DateConverter {
	private final static String DATE_PATTERN_KEY = "format.date";
	private final static String BUNDLE_NAME = "resources.ApplicationResources";
	public static Date convert(String source, Locale locale) throws NewsManagementException {
		ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_NAME, locale);
		String pattern = resources.getString(DATE_PATTERN_KEY);
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		Date resultDate;
		try{
			resultDate = formatter.parse(source);
		}
			catch (ParseException e) {
				throw new NewsManagementException(e);
		}
		return resultDate;
	}
}
